package batframework.code.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public abstract class IoUtils
{
	/**
	 * 
	 * @author bmyues
	 * @param in
	 * @param out
	 */
	public static void write(InputStream in,OutputStream out)
	{
		Assert.notNull(in, out);
		try {
			byte[] bytes = new byte[1024];
			int len = 0;
			while ((len = in.read(bytes)) != -1)
				out.write(bytes, 0, len);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 * @author bmyues
	 * @param data
	 * @param out
	 */
	public static void write(byte[] data,OutputStream out)
	{
		Assert.notNull(out);
		try {
			out.write(data);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * write file
	 * @author bmyues
	 * @param in
	 * @param file
	 */
	public static void write(InputStream in ,File file)
	{
		Assert.notNull(file);
		try {
			write(in, new FileOutputStream(file));
		} catch (FileNotFoundException e){
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * write file
	 * @author bmyues
	 * @param in
	 * @param file
	 */
	public static void write(InputStream in ,String pathName)
	{
		try {
			write(in, new FileOutputStream(new File(pathName)));
		} catch (FileNotFoundException e){
			throw new RuntimeException(e);
		}
	}
	
	/**
	 *  write file
	 * @author bmyues
	 * @param data
	 * @param pathName
	 */
	public static void write(byte[] data,String pathName)
	{
		try {
			write(data, new FileOutputStream(new File(pathName)));
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 
	 * @author bmyues
	 * @param input
	 * @return
	 */
	public static byte[] getBytes(InputStream input)
	{
		Assert.notNull(input);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			byte[] bytes = new byte[1024]; 
			int len = 0;
			while((len = input.read(bytes))!=-1)
				out.write(bytes, 0, len);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return out.toByteArray();
	}
	
	/**
	 * 
	 * @author bmyues
	 * @param file
	 * @return
	 */
	public static InputStream getInputStream(File file)
	{
		try{
			return new FileInputStream(file);
		} catch (FileNotFoundException e){
			throw new RuntimeException(e);
		}
	}
	
	public static boolean fileExists(String pathName)
	{
		return new File(pathName).exists();
	}
}
